n = int(input())
if n > 1000 and n <= 9999:
    a = [n % 10, n % 100 // 10, n % 1000 // 100, n // 1000, n % 100, n %1000 // 10, n // 100, n // 10, n %1000, n]
    z = [0]
    for i in range(9):
        s = 0
        for x in range(2, a[i]):
            if a[i] % x == 0:
                s = 1
        if s == 0:
            z.append(a[i])
    print(max(z))
else:
    print(0)